<!doctype html>
<html class="no-js">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <meta name="viewport"
        content="width=device-width, initial-scale=1">
  <title>Batch生成器</title>

  <!-- Set render engine for 360 browser -->
  <meta name="renderer" content="webkit">

  <!-- No Baidu Siteapp-->
  <meta http-equiv="Cache-Control" content="no-siteapp"/>

  <link rel="icon" type="image/png" href="assets/i/favicon.png">

  <!-- Add to homescreen for Chrome on Android -->
  <meta name="mobile-web-app-capable" content="yes">
  <link rel="icon" sizes="192x192" href="assets/i/app-icon72x72@2x.png">

  <!-- Add to homescreen for Safari on iOS -->
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="Amaze UI"/>
  <link rel="apple-touch-icon-precomposed" href="assets/i/app-icon72x72@2x.png">

  <!-- Tile icon for Win8 (144x144 + tile color) -->
  <meta name="msapplication-TileImage" content="assets/i/app-icon72x72@2x.png">
  <meta name="msapplication-TileColor" content="#0e90d2">

  <link rel="stylesheet" href="assets/css/amazeui.min.css">
  <link rel="stylesheet" href="assets/css/app.css">
  <style>
    .delete
    {
      cursor: pointer;
    }
  </style>
</head>
<body>

<header class="am-header">Batch生成器</header>

<div class="am-tabs" data-am-tabs="{noSwipe: 1}" id="doc-tab-demo-1">
  <ul class="am-tabs-nav am-nav am-nav-tabs">
    <li class="am-active"><a href="javascript: void(0)">积分加重</a></li>
    <li><a href="javascript: void(0)">倒序加重</a></li>
    <li><a href="javascript: void(0)">手动排位</a></li>
  </ul>

  <div class="am-tabs-bd">
    <div class="am-tab-panel am-active">
        <div name="ballast" id="ballst">
          <div class="b-setting am-form-group"> 
            <div class="">
              <label class="am-form-label">加重比率</label>
              <input class="" type="text" name="ballastRatio" id="ballastRatio">
            </div>
            <div class="">
              <label class="am-form-label">加重上限</label>
              <input type="text" name="ballastMaxium" id="ballastMaxium">
            </div>  
          </div>

          <hr>


          <form action="ballast.php" method="post">
            <div>

              <input type="button" name=""  onclick="createDriverBallst()" value="新增">
              <button type="button" onclick="deleteAllBallst()">删除全部</button>
              <button type="button" onclick="giveBallast()">生成</button>
              <button type="submit">下载ini</button>
            </div>
            <table border="0px" id="ballastTable">
              <tbody>
                <tr class="table-head">
                  <th width="60px">顺序</th>
                  <th width="150px">车手id</th>
                  <th width="50px">积分</th>
                  <th width="70px">加重(kg)</th>
                  <th width="50px"></th>
                </tr>
              </tbody>
            </table>
          </form>
        </div>
    </div>

    <div class="am-tab-panel">
        <div name="reverse" id="reverse">
          <div class="b-setting am-form-group"> 
              <div class="">
                <label class="am-form-label">倒序范围</label>
                <input class="" type="text" name="reverseRange" id="reverseRange">
              </div>
              <div class="">
                <label class="am-form-label">倒序间隔</label>
                <input type="text" name="reverseGap" id="reverseGap">
              </div>
              <div class="">
                <label class="am-form-label">加重最大值</label>
                <input type="text" name="reverseMax" id="reverseMax">
              </div>   
            </div>

            <hr>


            <form action="reverse.php" method="post">
              <div>
                <button type="button" onclick="createDriverReverse()">新增</button>
                <button type="button" onclick="deleteAllReverse()">删除全部</button>
                <button type="button" onclick="giveReverse()">生成</button>
                <button type="submit">下载ini</button>
              </div>
              <table border="0px" id="reverseTable">
                <tbody>
                  <tr class="table-head">
                    <th width="60px">顺序</th>
                    <th width="150px">车手id</th>
                    <th width="70px">加重(kg)</th>
                    <th width="50px"></th>
                  </tr>
                </tbody>
              </table>
            </form>
        </div>
    </div>

    <div class="am-tab-panel">
        <div name="grid" id="grid">
            <form action="grid.php" method="post">
              <div>
                <button type="button" onclick="createDriverGrid()">新增</button>
                <button type="button" onclick="deleteAllGrid()">删除全部</button>
                <button type="submit" onclick="test()">下载ini</button>
              </div>
              <table border="0px" id="gridTable">
                <tbody>
                  <tr class="table-head">
                    <th width="60px">顺序</th>
                    <th width="150px">车手id</th>
                    <th width="70px">加重(kg)</th>
                    <th width="50px"></th>
                  </tr>
                </tbody>
              </table>
            </form>
        </div>
    </div>
  </div>
</div>

</body>

<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="assets/js/amazeui.min.js"></script>
<!-- <script src="js/ballast.js"></script> -->
<script type="text/javascript">


var rowBallst=1;
var rowReverse=1;
var rowGrid=1;
var stock = new Array();


//新增一行加重

function createDriverBallst(){
  $("#ballst tr:last").after('<tr class="driver"></tr>');
  $("#ballst tr:last").append('<td class="row-id">'+rowBallst+'</td>');
  $("#ballst tr:last").append('<td><input type="text" name="name[]" class="name" style="width:140px;margin:0px;padding:0px;"></td>');
  $("#ballst tr:last").append('<td><input type="text" name="score[]" class="score" style="width:48px;margin:0px;padding:0px;"></td>');
  $("#ballst tr:last").append('<td><input type="text" name="weight[]" class="weight" style="width:50px;margin-left:5px;padding:0px;"></td>');
  $("#ballst tr:last").append('<td style="padding-left:10px"><span class="delete">删除</span></td>');
  rowBallst++;
  $("#ballst span.delete:last").click(deleteDriverBallst);
}

//新增一行倒序
function createDriverReverse() {
  $("#reverse tr:last").after('<tr class="driver"></tr>');
  $("#reverse tr:last").append('<td class="row-id">'+rowReverse+'</td>');
  $("#reverse tr:last").append('<td><input type="text" name="name[]" class="name" style="width:140px;margin:0px;padding:0px;"></td>');
  $("#reverse tr:last").append('<td><input type="text" name="weight[]" class="weight" style="width:50px;margin-left:5px;padding:0px;"></td>');
  $("#reverse tr:last").append('<td style="padding-left:10px"><span class="delete">删除</span></td>');
  rowReverse++;
  $("#reverse span.delete:last").click(deleteDriverReverse);
}

//新增一行排位
function createDriverGrid(){
  $("#grid tr:last").after('<tr class="driver"></tr>');
  $("#grid tr:last").append('<td class="row-id">'+rowGrid+'</td>');
  $("#grid tr:last").append('<td><input type="text" name="name[]" class="name" style="width:140px;margin:0px;padding:0px;"></td>');
  $("#grid tr:last").append('<td><input type="text" name="weight[]" class="weight" style="width:50px;margin-left:5px;padding:0px;"></td>');
  $("#grid tr:last").append('<td style="padding-left:10px"><span class="delete">删除</span></td>');
  rowGrid++;
  $("#grid span.delete:last").click(deleteDriverGrid);
}

//删除一行加重
function deleteDriverBallst(){
  $(this).parent().parent().remove();
  var i=0;
  $("#ballastTable tr").each(function(){
      if(i>=1){      
       $(this).children().eq(0).html(i);
      }
      i++;
  });
  rowBallst--;
}

//删除一行倒序
function deleteDriverReverse(){
  $(this).parent().parent().remove();
  var i=0;
  $("#reverseTable tr").each(function(){
      if(i>=1){      
       $(this).children().eq(0).html(i);
      }
      i++;
  });
  rowReverse--;
}

//删除一行排位
function deleteDriverGrid(){
  $(this).parent().parent().remove();
  var i=0;
  $("#gridTable tr").each(function(){
      if(i>=1){      
       $(this).children().eq(0).html(i);
      }
      i++;
  });
  rowGrid--;
}

//删除全部加重
function deleteAllBallst(){
  $("#ballst tr.driver").remove();
  rowBallst = 1;
}

//删除全部倒序
function deleteAllReverse(){
  $("#reverse tr.driver").remove();
  rowReverse = 1;
}

//删除全部排位
function deleteAllGrid(){
  $("#grid tr.driver").remove();
  rowGrid = 1;
}

//检查加重比率是否正确
function checkRatioBallst(){
  var br = $('#ballastRatio').val();
  br = Number(br);
  if (br<=0) {
    alert('加重比率不正确！');
    return false;
  }
  else{
    return true;
  };
}

//检车倒序参数是否已经设置
function checkRatioReverse(){
  var rr = $('#reverseRange').val();
  var rg = $('#reverseGap').val();
  rr = Number(rr);
  rg = Number(rg);
  if (rr<=0||rg<=0){
    alert('倒序参数设置错误！');
    return false;
  }
  else{
    return true;
  };
}

//检查是否有加重数据
function checkDataBallst(){
    if (rowBallst==1) {
    alert('没有添加数据');
    return false;
  }else{
    return true;
  }
}

//检查是否有倒序数据
function checkDataReverse(){
    if (rowReverse==1) {
    alert('没有添加数据');
    return false;
  }else{
    return true;
  }
}

//检查是否有排位数据
function checkDataGrid(){
    if (rowGrid==1) {
    alert('没有添加数据');
    return false;
  }else{
    return true;
  }
}

//给每行加重
function giveBallast(){
  // alert(1);
  // console.log($("#ballastTable").find(".driver"));
  // return;
  if (checkDataBallst()&&checkRatioBallst()) {
        //alert(2);
      $("#ballastTable").find(".driver").each(function()
      {
          var score = $(this).find("input:eq(1)").val();
          var ratio = $('#ballastRatio').val();
          var ballast = accMul(score,ratio);
          max = $('#ballastMaxium').val();
          //
          if (max!="") 
          {
              if (ballast <= max) 
              {
                  $(this).find("input:eq(2)").val(ballast);
              }
              else
              {
                  $(this).find("input:eq(2)").val(max);
              }
          }
          else
          {
              $(this).find("input:eq(2)").val(ballast);
          }
     }
  );
  }else{
    alert('数据不正确');
  }
}

//倒序加重操作
function giveReverse(){
  var range = $('#reverseRange').val();
  console.log(range);
  var gap = $('#reverseGap').val();
  var max = $('#reverseMax').val();
  var driver = new Array();
  $("#reverseTable").find(".driver").each(function(){
    if (range>0) {
       var name = $(this).find("input:eq(0)").val();
       driver.push(name); 
      }
      range--;
  });
  stock = driver;
  // console.log(driver);
  // console.log(driver.length);
  var driverLength = driver.length-1;
   $("#reverseTable").find(".driver").each(function(){
      if (driverLength>=0) {
         $(this).find("input:eq(0)").val(driver[driverLength]);
         $(this).find("input:eq(1)").val(max);
      }
    driverLength--;
    max = max - gap;
  });
  console.log(stock);
  $("#reverseTable .driver").find("input:eq(1)").each(function(){
      var weight = $(this).val();
      if (weight==""||weight<0) {
        $(this).val(0);
      }
  });
}

function test(){
  alert(1);
}

//防止出现小数点的乘法方法
function accMul(num1,num2){
    var m=0,s1=num1.toString(),s2=num2.toString(); 
    try{m+=s1.split(".")[1].length}catch(e){};
    try{m+=s2.split(".")[1].length}catch(e){};
    return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
 }

//曾经的直接写文件的方法
// function getData(){
//   var driver=new Array();
//    $(".driver").each(function()
//      {
//       var temp={
//         id:$(this).find("input:eq(0)").val(),
//         score:$(this).find("input:eq(1)").val(),
//         weight:$(this).find("input:eq(2)").val()
//     };
//       driver.push(temp);
//      }
//     );
//    //console.log(driver);
//    //console.log(JSON.stringify(driver));
//    return JSON.stringify(driver);
// }

// function postData()
// {
//  $.ajax({ 
//     type: "POST", 
//     url: "http://127.0.0.1/ba/d.php", 
//     contentType: "application/json; charset=utf-8", 
//     data: getData(), 
//     dataType: "json", 
//     success: function (message) { 
//     console.log(message);
//     savetxt('d.php?name=123');
//     }, 
//     error: function (message) { 
//     } 
// });  
// }
//     function savetxt(imgOrURL) {
//         if (typeof imgOrURL == 'object')
//             imgOrURL = imgOrURL.src;
//         window.win = open (imgOrURL);
//         setTimeout('win.document.execCommand("SaveAs")', 500);
//     }


</script>
</html>

